package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

/**
 * SROADDATA类
 * 实现设置然后读取对象属性数据的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class SROADDATA implements AxdrType {

    private byte[] dataCode = null;
    private OAD soad = null;
    private Data data = null;
    private OAD road = null;
    private Unsigned8 readDelay = null;

    public SROADDATA() {
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setAll(OAD soad, Data data, OAD road, Unsigned8 readDelay) {
        this.soad = soad;
        this.data = data;
        this.road = road;
        this.readDelay = readDelay;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        int codeLength = 0;

        this.soad = new OAD();
        codeLength += this.soad.decode(input);

        this.data = new Data();
        codeLength += this.data.decode(input);

        this.road = new OAD();
        codeLength += this.road.decode(input);

        this.readDelay = new Unsigned8();
        this.readDelay.setConst();
        codeLength += this.readDelay.decode(input);

        return codeLength;
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        int codeLength;

        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                try {
                    output.write(this.dataCode[i]);
                } catch (java.io.IOException e) {
                    throw new RuntimeException(e);
                }
            }
        } else {
            if (this.readDelay != null && this.road != null && this.data != null && this.soad != null) {
                codeLength = 0;
                codeLength += this.readDelay.encode(output);
                codeLength += this.road.encode(output);
                codeLength += this.data.encode(output);
                codeLength += this.soad.encode(output);
            } else {
                throw new RuntimeException("readDelay || road || data || soad is null");
            }
        }
        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        try {
            this.encode(revOStream);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException("Error encoding SROADDATA", e);
        }
        this.dataCode = revOStream.getArray();
    }

    @Override
    public String toString() {
        if (this.readDelay != null && this.road != null && this.data != null && this.soad != null) {
            return "sequence: {" + "soad: " + this.soad + ", data: " + this.data + ", road: " + this.road
                    + ", delayreadtime: " + this.readDelay + "}";
        } else {
            return "readDelay || road || data || soad is null";
        }
    }
}